package cn.lblbc.puzzle

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableIntState
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import cn.lblbc.puzzle.ui.theme.PuzzleTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            PuzzleTheme {
                // A surface container using the 'background' color from the theme
                Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
                    PuzzleView()
                }
            }
        }
    }
}

var drawableMap = mapOf(
    0 to R.drawable.pic_0,
    1 to R.drawable.pic_1,
    2 to R.drawable.pic_2,
    3 to R.drawable.pic_3,
    4 to R.drawable.pic_4,
    5 to R.drawable.pic_5,
    6 to R.drawable.pic_6,
    7 to R.drawable.pic_7,
    8 to R.drawable.pic_8
)

@Composable
fun PuzzleView() {
    val focusIndex = remember { mutableIntStateOf(-1) }
    val result = remember { mutableStateOf(false) }
    val variableList = remember { mutableStateListOf(0, 1, 2, 3, 4, 5, 6, 7, 8) }
    Column(
        modifier = Modifier.padding(top = 50.dp)
    ) {
        Row(modifier = Modifier.align(alignment = Alignment.CenterHorizontally)) {
            BuildIcon(variableList, result, focusIndex, 0)
            BuildIcon(variableList, result, focusIndex, 1)
            BuildIcon(variableList, result, focusIndex, 2)
        }
        Row(modifier = Modifier.align(alignment = Alignment.CenterHorizontally)) {
            BuildIcon(variableList, result, focusIndex, 3)
            BuildIcon(variableList, result, focusIndex, 4)
            BuildIcon(variableList, result, focusIndex, 5)
        }
        Row(modifier = Modifier.align(alignment = Alignment.CenterHorizontally)) {
            BuildIcon(variableList, result, focusIndex, 6)
            BuildIcon(variableList, result, focusIndex, 7)
            BuildIcon(variableList, result, focusIndex, 8)
        }
        Row(
            modifier = Modifier
                .align(alignment = Alignment.CenterHorizontally)
                .padding(top = 20.dp)
        ) {
            Button(
                onClick = {
                    focusIndex.intValue = -1
                    val list = drawableMap.keys.shuffled()
                    for (i in 0..8) {
                        variableList[i] = list[i]
                    }
                }
            ) {
                Text(text = "开始")
            }
        }
        MyAlertDialog(result)
    }
}

@Composable
private fun BuildIcon(variableList: SnapshotStateList<Int>, result: MutableState<Boolean>, focusIndex: MutableIntState, index: Int) {
    Image(painter = painterResource(drawableMap[variableList[index]]!!),
        contentDescription = "",
        modifier = Modifier
            .size(90.dp)
            .background(getBackgroundColor(focusIndex, index))
            .padding(2.dp)
            .clickable { onClickIcon(variableList, result, focusIndex, index) })
}

fun onClickIcon(variableList: SnapshotStateList<Int>, result: MutableState<Boolean>, focusIndex: MutableIntState, index: Int) {
    if (focusIndex.intValue != -1) {
        if (focusIndex.intValue != index) {
            val tmp = variableList[focusIndex.intValue]
            variableList[focusIndex.intValue] = variableList[index]
            variableList[index] = tmp
            checkResult(variableList, result)
        }
        focusIndex.intValue = -1
    } else {
        focusIndex.intValue = index
    }
}

fun checkResult(variableList: SnapshotStateList<Int>, result: MutableState<Boolean>) {
    var checkResult = true
    for (i in variableList.indices) {
        if (variableList[i] != i) {
            checkResult = false
            break
        }
    }
    result.value = checkResult
}

@Composable
fun MyAlertDialog(result: MutableState<Boolean>) {
    if (result.value) {
        AlertDialog(onDismissRequest = {},
            text = {
                Text(text = "恭喜，拼好啦！")
            },
            confirmButton = {
                Button(onClick = { result.value = false }) {
                    Text(text = "确定")
                }
            }
        )
    }
}

@Composable
private fun getBackgroundColor(focusIndex: MutableIntState, index: Int): Color {
    if (focusIndex.intValue == index) {
        return Color.Yellow
    } else {
        return Color.White
    }
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    PuzzleTheme {
        PuzzleView()
    }
}